#!/bin/bash
SCRIPT_ABS_FILENAME=`LC_ALL=en_US.ISO8859-1 perl -e 'use Cwd "abs_path";print abs_path(shift)' "${BASH_SOURCE[0]}"`
SCRIPT_DIR=`dirname "$SCRIPT_ABS_FILENAME"`

#SCRIPT_DIR="$(pwd)" # copy in terminal to be able to copy and paste command from this file
cd "$SCRIPT_DIR"

OSTYPE=`uname -s`
echo $OSTYPE

#env
#echo "$@"

project_dir="$1"
echo project_dir="$project_dir"
executable="$2"
echo executable="$executable"
executable_name=$(basename "$executable")
echo executable_name="$executable_name"
executable_name_without_extension="${executable_name%.*}"
echo executable_name_without_extension="$executable_name_without_extension"
executable_path="$(dirname "$executable")"
echo executable_path="$executable_path"
executable_dirname="${executable_path##*/}"
echo executable_dirname="$executable_dirname"



if [ "$OSTYPE" = "Linux" ]
then
  : # todo
fi



qemu_path=./qemu_portable-v4.2.0/qemu-system-x86_64
qemu_path=./qemu_portable-v5.2.0/qemu-system-x86_64
#set -x

if [ "$OSTYPE" = "Darwin" ]
then
  if ! [ -f "$qemu_path" ]
  then
    echo "You must restore the folder \'"$(dirname "$qemu_path")"\'"
    exit 1
  fi
  if ! [ -f ./disk_image_gpt.img ]
  then
    unzip -o ./disk_image_gpt.img.zip -d .
    rm -rf ./__MACOSX
  fi

  if pgrep qemu
  then
    killall qemu-system-x86_64
  fi
  > ./serial0_qemu_out.log # empty file without erasing it. Useful for editor for refreshing and reloading.

  hdiutil attach ./disk_image_gpt.img || exit 1

  shopt -s nocasematch

  if [[ "$executable_name" == "CloverX64"* ]]
  then
    if [[ "$executable_dirname" == "DWARF" ]]
    then
        echo ditto "$project_dir"/"$(dirname "$executable")"/../../../../"$executable_name_without_extension".efi /Volumes/QEMU_EFI/EFI/CLOVER/"$executable_name_without_extension".efi
           ditto "$project_dir"/"$(dirname "$executable")"/../../../../"$executable_name_without_extension".efi /Volumes/QEMU_EFI/EFI/CLOVER/"$executable_name_without_extension".efi || exit 1
    else
        echo ditto "$project_dir"/"$executable_path"/"$executable_name_without_extension".efi /Volumes/QEMU_EFI/EFI/CLOVER/"$executable_name_without_extension".efi
           ditto "$project_dir"/"$executable_path"/"$executable_name_without_extension".efi /Volumes/QEMU_EFI/EFI/CLOVER/"$executable_name_without_extension".efi || exit 1
    fi
  else
    : # if it's a module, has to be copied in Drivers
  fi

  shopt -u nocasematch

  diskutil eject /Volumes/QEMU_EFI || exit 1

  sleep 1 # not 100% sure it's needed
  set -m

  bios_file=("$(dirname "$qemu_path")"/bios*)
    
  "$qemu_path" \
    -L "$(dirname "$qemu_path")" \
    -m 2048 \
    -cpu Penryn \
    -bios "$bios_file" \
    -machine q35 \
    -device ahci,id=ahi \
    -drive format=raw,id=hda,file=./disk_image_gpt.img \
    -usb \
    -device usb-mouse,bus=usb-bus.0,port=2 \
    -device usb-kbd,bus=usb-bus.0,port=1 \
    -serial file:./serial0_qemu_out.log \
    -nic none \
    -gdb tcp::9000 &
    [[ $? == 0 ]] || exit 1

  set +m

#    -cdrom "/JiefLand/3.Infos/3.Infos-Mac/MacOs/11.0.20A5343j Big Sur/BigSurInstall.iso" \


  count=1
  timeout=60
  image_base_str="CloverX64 : Image base = "

    getImageBase() {
    [ -f ./serial0_qemu_out.log ] || return 1
    grep "$image_base_str" ./serial0_qemu_out.log
    return $?
  }


  while [ $count -le $timeout ] && ! getImageBase
  do
    sleep 1
    count=$((count+1))
  done

  if [ $count -ge $timeout ]
  then
    echo Cannot find "\"Clover : Image base = \"" in ./serial0_qemu_out.log after $timeout seconds
    exit 1
  fi

  adr="$(grep "$image_base_str" ./serial0_qemu_out.log | awk '{printf "0x%x",$NF}')"
  echo adr="$adr"


  echo ovh.knudsen.jf.launch.vmware.efi.adr="$adr"
  exit 0



fi

# Windows ???
#if [ "$OSTYPE" = "FreeBSD" ]
#then
#fi

